<?php

namespace App\Http\Controllers;

use App\Http\Classes\Exception;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function response($code = 1, $message = '', $data = [], $error = null){
        $response = [];
        $response['code'] = $code;
        $response['message'] = $message;
        $response['data'] = $data;
        $response['error'] = $error;
        return response()->json($response);
    }

    public function exceptionResponse(\Exception $exception, $function = __FUNCTION__)
    {
        $res = [];
        $res['code'] = $exception->getCode();
        $res['function'] = $function;
        $res['file'] = $exception->getFile();
        $res['line'] = $exception->getLine();
        $res['message'] = $exception->getMessage();
//        $res['previous'] = $exception->getPrevious();
//        $res['trace'] = $exception->getTraceAsString();
        $res['ip'] = $_SERVER['REMOTE_ADDR'];

        $E = new Exception();
        $E->logException($res);

        return $this->response(503, 'Server internal error', null, $res);
    }

    public function exceptionView(\Exception $exception, $function = __FUNCTION__, $message = '')
    {
        $res = [];
        $res['code'] = $exception->getCode();
        $res['function'] = $function;
        $res['file'] = $exception->getFile();
        $res['line'] = $exception->getLine();
        $res['message'] = $exception->getMessage();
//        $res['previous'] = $exception->getPrevious();
//        $res['trace'] = $exception->getTraceAsString();
        $res['ip'] = $_SERVER['REMOTE_ADDR'];

        $E = new Exception();
        $E->logException($res);

        if ( $message !== '' )
        {
            $res['extra'] = $message;
        }

        return view('errors.503')->with('error', $res);
    }
}
